package com.k1998.mybatis.service.impl;

import com.k1998.mybatis.bean.JxTimeTable;
import com.k1998.mybatis.dao.IJxTimeTableDao;
import com.k1998.mybatis.service.JxTimeTableService;
import com.k1998.mybatis.service.QiniuService;
import com.k1998.mybatis.utils.BatchInfo;
import com.k1998.mybatis.utils.HashMapUtils;
import com.k1998.mybatis.utils.JxUtils;
import com.k1998.mybatis.utils.QueryInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * @Author: ChenYuxiang
 * @Description: //JxTimeTableImpl
 * @Param:
 * @return:
 * @Date: 17:24 2021/2/25
 **/
@Service("JxTImeTableService")
public class JxTimeTableImpl implements JxTimeTableService {
    @Autowired
    IJxTimeTableDao iJxTimeTableDao;

    @Autowired
    QiniuService qiniuService;

    public String findAll( QueryInfo queryInfo ){
        if(null==queryInfo.getQuery()){queryInfo.setQuery(null);}
        int numbers = iJxTimeTableDao.getCounts("%"+queryInfo.getQuery()+"%");
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<JxTimeTable> jx = iJxTimeTableDao.getAll("%"+queryInfo.getQuery()+"%",pageStart,queryInfo.getPageSize());
        return HashMapUtils.toHashMap(numbers,jx);
    }

    @Override
    public String detail(JxTimeTable jxTimeTable) {
        return HashMapUtils.toHashMap(iJxTimeTableDao.detail(jxTimeTable.getId()));
    }

    @Override
    public String delete(BatchInfo batchInfo) {
        List<String> list=JxUtils.stringToList(batchInfo.getIds());
        if(null==list){
            return "error";
        }else{
            iJxTimeTableDao.delete(list);
            return "success";
        }
    }

    @Override
    public String insert(MultipartFile multipartFile,String name ,Integer order,String remark) throws IOException {
        JxTimeTable jxTimeTable=new JxTimeTable();
        jxTimeTable.setClass_name(name);
        jxTimeTable.setClass_order(order);
        jxTimeTable.setRemark(remark);
        String url="http://"+qiniuService.uploadFile(multipartFile.getInputStream());
        jxTimeTable.setUrl(url);
        return iJxTimeTableDao.insert(jxTimeTable)>0 ? "success" : "error";
    }

    @Override
    public String update(MultipartFile multipartFile, Integer id, String name , Integer order, String remark) throws IOException {
        JxTimeTable jxTimeTable=new JxTimeTable();
        jxTimeTable.setId(id);
        jxTimeTable.setClass_name(name);
        jxTimeTable.setClass_order(order);
        jxTimeTable.setRemark(remark);
        if(!Objects.isNull(multipartFile)){
            jxTimeTable.setUrl("http://"+qiniuService.uploadFile(multipartFile.getInputStream()));
        }
        return iJxTimeTableDao.update(jxTimeTable)>0 ? "success" : "error";
    }

}
